package org.nbict.iot.task.once.schedule;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 *
 * Created by songseven on 18/7/17.
 */
public class Scheduler {

    //两个线程导致锁竞争
    public static void main(String[] args) throws InterruptedException {

        ScheduledExecutorService reducer = Executors.newScheduledThreadPool(1);
        ScheduledExecutorService dump = Executors.newScheduledThreadPool(1);

        //测试
        Long runningDelay =  5 * 60l; //5分钟
        Long dumpDelay = 60 * 60l; //60分钟

        reducer.scheduleWithFixedDelay(new RunningScheduler(), 5l, runningDelay, TimeUnit
                .SECONDS); //每隔5分钟执行一次

        dump.scheduleAtFixedRate(new DumpRunning(), 60l, dumpDelay, TimeUnit.SECONDS); //每隔一小时执行一次dump

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                //TODO
                reducer.shutdown();
                dump.shutdown();
            }
        });
    }
}
